package com.ymatou.database.wrapper;

import com.ymatou.database.model.HunterRelation;
import com.ymt.database.SqlManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HunterRelationWrapper {
	
	SqlManager manager;

    public HunterRelationWrapper() {
        manager = new SqlManager("hunter");
    }
    
    @SuppressWarnings("unchecked")
	public HunterRelation selectByPrimaryKey(Long hunterId) {
    	
    	HunterRelation hunterRelation = new HunterRelation();
    	List<HunterRelation> result = manager
                .getSelectResults(
                        "com.ymatou.database.mapper.HunterRelationMapper.selectByPrimaryKey",
                        hunterId);
        manager.commit();
        
		if (result.size() > 0) {
			hunterRelation = result.get(0);
		} else
			return null;
		
        return hunterRelation;
    }
    
    // 获取某个推手的所有下级
 	@SuppressWarnings("unchecked")
	public List<HunterRelation> selectSubordinateByHunterId(Long parentHunterId) {

 		List<HunterRelation> result = manager
 				.getSelectResults(
 						"com.ymatou.database.mapper.HunterRelationMapper.selectSubordinateByHunterId",
 						parentHunterId);
 		manager.commit();

 		if (result.size() == 0) {
 			return null;
 		}

 		return result;
 	}
 	
 	// 获取某个推手的所有下级
  	@SuppressWarnings("unchecked")
  	public List<HunterRelation> selectSubordinateByHunterIdWithLimit(Long parentHunterId) {

  		List<HunterRelation> result = manager
  				.getSelectResults(
  						"com.ymatou.database.mapper.HunterRelationMapper.selectSubordinateByHunterIdWithLimit",
  						parentHunterId);
  		manager.commit();

  		if (result.size() == 0) {
  			return null;
  		}

  		return result;
  	}
  	
  	// 通过code获取某个推手
   	@SuppressWarnings("unchecked")
   	public HunterRelation selectHunterInfoByCode(String code) {

   		List<HunterRelation> result = manager
   				.getSelectResults(
   						"com.ymatou.database.mapper.HunterRelationMapper.selectHunterInfoByCode",
   						code);
   		manager.commit();

   		if (result.size() == 0) {
   			return null;
   		}

   		return result.get(0);
   	}
 	
	// 通过下级名称和hunterId获取下级
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public HunterRelation selectSubordinateByHunterIdAndSubordinateName(
			Long hunterId, String subordinateName) {

		HunterRelation hunterRelation = new HunterRelation();

		Map m = new HashMap<String, String>();

		m.put("parent_hunter_id", hunterId);
		m.put("hunter_name", subordinateName);

		List<HunterRelation> result = manager
				.getSelectResults(
						"com.ymatou.database.mapper.HunterRelationMapper.selectSubordinateByHunterIdAndSubordinateName",
						m);
		manager.commit();

		if (result.size() > 0) {
			hunterRelation = result.get(0);
		}

		return hunterRelation;
	}
	
	// 通过下级id和hunterId获取下级
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public HunterRelation selectSubordinateByHunterIdAndSubordinateId(
			Long hunterId, Long subordinateUserId) {

		HunterRelation hunterRelation = new HunterRelation();

		Map m = new HashMap<String, String>();

		m.put("parent_hunter_id", hunterId);
		m.put("hunter_id", subordinateUserId);

		List<HunterRelation> result = manager
				.getSelectResults(
						"com.ymatou.database.mapper.HunterRelationMapper.selectSubordinateByHunterIdAndSubordinateId",
						m);
		manager.commit();

		if (result.size() > 0) {
			hunterRelation = result.get(0);
		}

		return hunterRelation;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<HunterRelation> selectHunterContributionList(Long hunterId) {

		Map m = new HashMap<String, String>();

		m.put("hunterId", hunterId);

		List<HunterRelation> result = manager
				.getSelectResults(
						"com.ymatou.database.mapper.HunterRelationMapper.selectHunterContributionList",
						m);
		manager.commit();

		return result;
	}
 	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public HunterRelation updateLevelByHunterId(
			Long hunterId, int level) {

		HunterRelation hunterRelation = new HunterRelation();

		Map m = new HashMap<String, Object>();

		m.put("hunter_id", hunterId);
		m.put("hunter_level", level);

		List<HunterRelation> result = manager
				.getSelectResults(
						"com.ymatou.database.mapper.HunterRelationMapper.updateLevelByHunterId",
						m);
		manager.commit();

		if (result.size() > 0) {
			hunterRelation = result.get(0);
		}

		return hunterRelation;
	}
    
    public static void main(String[] args) {
    	HunterRelationWrapper test = new HunterRelationWrapper();
        System.out.println(test.updateLevelByHunterId(25114532L, 2).getChildren_total());
        // test.insertYmtCouponSetting(3,11);
        // Logger.debug(test.getLastYmtCouponSettingId());

    }
}
